package com.aq.appserver.server.theme.dao;

import java.util.Date;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.aq.appserver.server.theme.entity.Theme;

public interface ThemeDao extends PagingAndSortingRepository<Theme, Long> , JpaSpecificationExecutor<Theme>{

	@Modifying
	@Query("update Theme s set s.clickNum = s.clickNum + 1 where s.id = ?1")
	public void updateClickNum(long themeId);

	@Query("select new com.aq.appserver.server.theme.entity.Theme(s.id, s.title, s.content, s.url, s.phoneNum, s.position, s.clickNum, s.anonymousTag,"
			+ "  a.nickName, s.updateTime, s.topTime, s.isTop) from Theme s, Account a where s.phoneNum = a.phoneNumber and s.title like ?1")
	public Page<Theme> findThemeByTitleLike(String title, Pageable pageable);

	@Query("select new com.aq.appserver.server.theme.entity.Theme(s.id, s.title, s.content, s.url, s.phoneNum, s.position, s.clickNum, s.anonymousTag,"
			+ "  a.nickName, s.updateTime, s.topTime, s.isTop) from Theme s, Account a where s.phoneNum = a.phoneNumber and s.title like ?2 and s.phoneNum = ?1")
	public Page<Theme> findThemeByphoneNumAndTitleLike(String phoneNum,
			String title, Pageable pageable);

	@Query("select new com.aq.appserver.server.theme.entity.Theme(s.id, s.title, s.content, s.url, s.phoneNum, s.position, s.clickNum, s.anonymousTag,"
			+ "  a.nickName, s.updateTime, s.topTime, s.isTop) from Theme s, Account a where s.phoneNum = a.phoneNumber and s.phoneNum = ?1")
	public Page<Theme> findThemeByphoneNum(String phoneNum, Pageable pageable);

	@Query("select count(distinct s.id) from Theme s  where s.phoneNum = ?1")
	public int getMyHelpCount(String phoneNumber);

	@Query("select new com.aq.appserver.server.theme.entity.Theme(s.id, s.title, s.content, s.url, s.phoneNum, s.position, s.clickNum, s.anonymousTag,"
			+ "  a.nickName, s.updateTime, s.topTime, s.isTop) from Theme s, Account a where s.phoneNum = a.phoneNumber")
	public Page<Theme> findThemeList(Pageable pageable);

	@Modifying
	@Query("update Theme s set s.updateTime = ?2 where s.id = ?1")
	public void updateThemeTime(long themeId, Date date);
	
	@Modifying
	@Query("update Theme s set s.topTime = ?2 where s.id = ?1")
	public void updateTopTime(long themeId, String topTime);
	
	@Modifying
	@Query("update Theme s set s.isTop = ?2, s.topTime = ?3 where s.id = ?1")
	public void updateTop(long themeId, String isTop, String topTime);
	
	@Query("select count(distinct s.id) from Theme s where s.isTop = 1")
	public int queryTopNum();
}